package com.icss.ah.fullGift.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.Json;
import com.icss.ah.fullGift.service.FullGiftService;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.j2ee.util.UUID;

@Controller
public class FullGiftAction extends BaseAction {

	private static final long serialVersionUID = 1324L;
	@Autowired
	private FullGiftService fullGiftService;
	
	private String fgId;
	private String fgName;
	private String fgRange;
	private String timesFg;
	private String begTime;
	private String endTime;
	private String useType;
	private String useCondition;
	private String status;
	private String flag;
	private String proIds;
	private String useCalcType;	//使用条件计算方式1单品分别计算2多商品混合计算
	private String giftType;	//赠品类型1同种商品(当USE_CALC_TYPE=2时不可选)2自定义
	
	private String firstSeries;
	private String secondSeries;
	private String title;
	private String minPriceStr;
	private String maxPriceStr;
	
	public String toFullGiftListPage(){
		return "toFullGiftListPage";
	}
	
	public void queryFullGiftList(){
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId",corpId);	//公众号id
		map.put("shopId", shopId);	//商铺id
		map.put("fgName", fgName);
		map.put("begTime", begTime);
		map.put("status", status);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		dataGridJson = fullGiftService.queryFullGiftList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
		
	}
	
	public String toFullGiftDetailPage(){
		String title = "满赠活动详情";
		if("add".equals(flag)) title = "新增满赠活动";
		else if("update".equals(flag)) title = "编辑满赠活动";
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId",corpId);	//公众号id
		map.put("shopId", shopId);	//商铺id
		map.put("fgId", fgId);
		Map<String, Object> bean = fullGiftService.queryFullGiftInfo(map);
		map.put("level", "1");
		List<Map<String, Object>> yjfl = fullGiftService.queryProductTypeList(map);
		request = getRequest();
		request.setAttribute("title", title);
		request.setAttribute("bean", bean);
		request.setAttribute("yjfl", yjfl);
		request.setAttribute("flag", flag);
		request.setAttribute("fgId", fgId);
		return "toFullGiftDetailPage";
	}
	
	public void queryProductList(){
		String serise ="";
		if(secondSeries==null||"".equals(secondSeries)||"0".equals(secondSeries)||"null".equals(secondSeries)){
			if(firstSeries!=null&&!"null".equals(firstSeries)&&!"0".equals(firstSeries)) 
			serise = firstSeries==null?"":firstSeries;
		}else{
			 serise = secondSeries;
		}
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId",corpId);	//公众号id
		map.put("shopId", shopId);	//商铺id
		map.put("title", title);
		map.put("minPriceStr", minPriceStr);
		map.put("maxPriceStr", maxPriceStr);
		map.put("serise",serise);
		map.put("fgId",fgId);
		List<Map<String, Object>> list = fullGiftService.queryProductList(map);
		str2Resp(JSONArray.fromObject(list).toString());
	}
	
	public void save(){
		if(flag!=null && "add".equals(flag)) fgId = new UUID().toString();
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("fgId", fgId);
		map.put("shopId", shopId);
		map.put("corpId", corpId);
		map.put("fgName", fgName);
		map.put("fgRange", fgRange);
		map.put("timesFg", timesFg);
		map.put("begTime", begTime);
		map.put("endTime", endTime);
		map.put("useType", useType);
		map.put("useCalcType", useCalcType);
		map.put("giftType", giftType);
		map.put("useCondition", useCondition);
		map.put("flag", flag);
		map.put("status", status);
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		if(proIds!=null && !"".equals(proIds)){
			String[] proIdArr = proIds.split(",");
			for(String proId : proIdArr){
				Map<String, String> pro = new HashMap<String, String>();
				pro.put("fgProId", new UUID().toString());
				pro.put("shopId", shopId);
				pro.put("corpId", corpId);
				pro.put("proId", proId);
				pro.put("fgId", fgId);
				list.add(pro);
			}
		}
		boolean success = fullGiftService.save(map, list);
		jsonText = new Json();
		jsonText.setSuccess(success);
		jsonText.setMsg(fgId);
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
	
	public void updateStatus(){
		fullGiftService.updateFullGiftInfoStatus(fgId);
	}
	
	private String level;
	public void queryProductTypeList(){
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		Map<String, String> map = new HashMap<String, String>();
		map.put("corpId",corpId);	//公众号id
		map.put("shopId", shopId);	//商铺id
		map.put("firstSeries", firstSeries);
		map.put("level", level);
		List<Map<String, Object>> list = fullGiftService.queryProductTypeList(map);
		str2Resp(JSONArray.fromObject(list).toString());
	}
	
	public void delInfo(){
		String result = fullGiftService.delInfo(fgId);
		str2Resp(result);
	}

	public String getFgId() {
		return fgId;
	}

	public void setFgId(String fgId) {
		this.fgId = fgId;
	}

	public String getFgName() {
		return fgName;
	}

	public void setFgName(String fgName) {
		this.fgName = fgName;
	}

	public String getFgRange() {
		return fgRange;
	}

	public void setFgRange(String fgRange) {
		this.fgRange = fgRange;
	}

	public String getTimesFg() {
		return timesFg;
	}

	public void setTimesFg(String timesFg) {
		this.timesFg = timesFg;
	}

	public String getBegTime() {
		return begTime;
	}

	public void setBegTime(String begTime) {
		this.begTime = begTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getUseType() {
		return useType;
	}

	public void setUseType(String useType) {
		this.useType = useType;
	}

	public String getUseCondition() {
		return useCondition;
	}

	public void setUseCondition(String useCondition) {
		this.useCondition = useCondition;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getProIds() {
		return proIds;
	}

	public void setProIds(String proIds) {
		this.proIds = proIds;
	}

	public String getFirstSeries() {
		return firstSeries;
	}

	public void setFirstSeries(String firstSeries) {
		this.firstSeries = firstSeries;
	}

	public String getSecondSeries() {
		return secondSeries;
	}

	public void setSecondSeries(String secondSeries) {
		this.secondSeries = secondSeries;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMinPriceStr() {
		return minPriceStr;
	}

	public void setMinPriceStr(String minPriceStr) {
		this.minPriceStr = minPriceStr;
	}

	public String getMaxPriceStr() {
		return maxPriceStr;
	}

	public void setMaxPriceStr(String maxPriceStr) {
		this.maxPriceStr = maxPriceStr;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getUseCalcType() {
		return useCalcType;
	}

	public void setUseCalcType(String useCalcType) {
		this.useCalcType = useCalcType;
	}

	public String getGiftType() {
		return giftType;
	}

	public void setGiftType(String giftType) {
		this.giftType = giftType;
	}

}



